package cs5226.tags;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import cs5226.util.*;
import cs5226.model.*;

public class ThresholdDisplay extends SimpleTagSupport {
	
	private String statistics;

	@Override
	public void doTag() throws JspException, IOException {
		JspWriter out = getJspContext().getOut();
		
		if (StringUtil.isNullOrBlank(statistics)) {			
			out.println ("No statistics specified!");
			return;
		}
		
		PageContext pc = (PageContext) getJspContext();
		HttpServletRequest request = (HttpServletRequest) pc.getRequest();
		
		String output = "- Threshold setting: ";
		try {
			ThresholdBean threshold = ThresholdUtil.getThresholdValues(statistics, request);
			request.setAttribute(statistics + "-Threshold", threshold);
			double min = threshold.getMin();
			double max = threshold.getMax();
			double unhealthy = threshold.getUnhealthy();
			double critical = threshold.getCritical();
			if (unhealthy < critical) {
				output += "<span style='color:green'>[" + min + " - " + unhealthy + "): Normal </span> &laquo; "; 
				output += "<span style='color:yellow'>[" + unhealthy + " - " + critical + "): Warning </span> &laquo; "; 
				output += "<span style='color:red'>[" + critical + " - " + max + "): Warning </span>"; 
			}
			else {
				output += "<span style='color:red'>[" + min + " - " + critical + "): Critical </span> &laquo; "; 
				output += "<span style='color:yellow'>[" + critical + " - " + unhealthy + "): Warning </span> &laquo; "; 
				output += "<span style='color:green'>[" + unhealthy + " - " + max + "): Normal </span>"; 
			}
			
			
			out.println (output);
		}
		catch (Exception e) {
			e.printStackTrace();
			out.println("error during rendering threshold setting for statistics '" + statistics + "'");
		}
	}

	public String getStatistics() {
		return statistics;
	}

	public void setStatistics(String statistics) {
		this.statistics = statistics;
	}
	
}
